Repository navigation
fix(serve): serve the current graph while graph.json reloads (#5371) - #5374
Deepjyoti-Sarmah wants to merge 3 commits into
Conversation
Drive the server's own request handlers concurrently with a slow _load_entry. On v8 every request waits out the reload, because call_tool and read_resource load the graph on the event loop.
…urce call_tool and read_resource called _select_graph() -> _ctx_cache.load() synchronously on the event loop. A reload of a large graph.json stopped every other request, so the stale entry Graphify-Labs#5181 hands concurrent callers was never reachable (Graphify-Labs#5371). Run the load in a worker thread first, then bind G with the synchronous _select_graph, which is now a cache hit or the current entry. The handler still runs with no await after the bind, so another call cannot swap G mid-handler.
|
Thanks for the pull request, @Deepjyoti-Sarmah. A maintainer will review it soon. Want to talk it through while it is in review? Come join us on our Discord server. For longer-form discussion there is also GitHub Discussions. A couple of things that speed up review: make sure the test suite passes on Python 3.11 and 3.13, and that the change keeps extraction deterministic. |
There was a problem hiding this comment.
Graphify code review 
Review completed · 8bbc794bf70a
Note
Merge confidence: 5/5 · Nothing blocking · 4 possible issues to check · 🟠 High impact
4 possible issues not confirmed
7 not re-checked
Tool calls and resource reads now load or reload their graph in a worker thread before selecting the graph for the request.
Full summary
Tool calls and resource reads now load or reload their graph in a worker thread before selecting the graph for the request. The request handler still runs synchronously after graph selection.
No confirmed issues. We found 4 possible issues we couldn't confirm; see below.
Not confirmed (4 possible issues)
Leads worth a look; we didn't double-check these. They don't change the score.
Not confirmed · The project-call timing test can pass without slowing the reload when the graph path does not match the substring filter Inspect source
Detailed review · not confirmed
The slow-load filter matches slow_only as a substring of the resolved path. If the resolved path is spelled differently from tmp_path (for example through a symlink), the filter never matches, no load is slowed, and the test passes without checking the behavior it is meant to guard.
slow at line 33 only sleeps when slow_only in resolved_path, and line 104 passes str(tmp_path / "graph.json") unnormalized. The test then asserts only that the project call took less than DELAY/2 at line 121 and never checks that the default reload took DELAY, so a non-match leaves nothing to fail.
-
Not checked whether tmp_path is already resolved on the test platform
-
Not checked how
_load_entryformats resolved_path -
Found by the per-file sweep; not double-checked.
Not confirmed · The concurrent tool-call test is named for serving the current graph but never checks the returned content Inspect source
Detailed review · not confirmed
The test named for serving the current graph during a reload asserts only timing. It never checks which graph text the non-triggering calls returned.
Line 86 discards the response text returned by _send, and line 88 asserts only on times. The test would not catch a regression that returns the wrong graph quickly, even though the title and the module docstring say the calls are answered from the current graph.
-
Not checked what
graph_statsreturns for GRAPH with one node -
Timing thresholds of DELAY/2 may be flaky on a heavily loaded CI host
-
Found by the per-file sweep; not double-checked.
Not confirmed · Project graph can be evicted during the background load, so the event loop still rebuilds it and stalls other requests Inspect source
Detailed review · not confirmed
The _warm_graph offload only helps if the graph is still cached when _select_graph runs. Other requests can run during the await and load other project graphs, evicting this one from the bounded LRU. Then _select_graph rebuilds the graph synchronously on the event loop, which is the stall #5371 was meant to remove.
_warm_graph at line 2989 only calls _load_ctx and discards the result. _select_graph at line 3535 (and at line 2970 and line 3477) then calls _load_ctx again on the loop thread, without any await in between. The docstring at lines 2980-2981 assumes _select_graph is always a cache hit or sees an in-flight entry, but nothing guarantees the entry survives the await. The LRU bound comes from _max_server_contexts(), which is not shown.
-
The eviction policy and capacity of
_GraphContextCacheand the value returned by_max_server_contexts()are not in the inspected range. -
Whether
_ctx_cache.loadrebuilds or blocks on a stale entry is not shown. -
Found by the per-file sweep; not double-checked.
Not confirmed · Graph cache is now loaded from several worker threads at once, so it can race and rebuild the same graph repeatedly Inspect source
Detailed review · not confirmed
Before this change every load ran on the event loop, so calls were serialized. Now _warm_graph runs _load_ctx in worker threads, so concurrent calls can enter the cache at the same time. Concurrent loads of the same stale graph can each rebuild it, and the cache's internal bookkeeping may be unsafe under concurrent access.
anyio.to_thread.run_sync(_load_ctx, ...) at line 2989 has no per-path coalescing or lock. The comment at lines 2953-2957 says the event loop serialized access to the shared context, which no longer holds for the load. The cache's locking is not in the inspected range.
-
The
_GraphContextCacheimplementation is not in the inspected range, so whether it uses a lock or single-flight loading is unknown. -
Actual interleaving depends on the anyio worker thread limit, which is not shown.
-
Found by the per-file sweep; not double-checked.
What this review covered
- Some lookups asked for files or lines that do not exist; the review was told and carried on.
- 2 of 2 changed files inspected. This is scope, not a correctness guarantee.
Code graph and static checks · 881 functions depend on this change
Impact & health
Impact — 881 functions depend on the 226 functions this change touches.
Health — this change adds coupling hotspots:
- worse:
_build_server()— 3 callers, 22 callees - worse:
read_resource()— 1 callers, 8 callees
Formal checks — 881 functions this change can affect weren't formally checked in this review. Formal checks are informational only.
Gate & verification
✅ PASS — objectively clean (no health regressions, tests not run — proofs not run this pass (advisory)). Grounded, not self-assessed.
Advisory (not blocking):
- verification_scope: 650 function(s) in the blast radius were not formally verified this run
Test selection
29 of 608 test file(s) selected (5%) via static blast radius.
tests/test_4874_jvm_resolution.py— impacttests/test_affected_cli.py— impacttests/test_affected_path_file_node.py— impacttests/test_affected_seed_reference_forms.py— impacttests/test_benchmark.py— impacttests/test_benchmark_raw_graph.py— impacttests/test_benchmark_real_query.py— impacttests/test_confidence_score_output.py— impacttests/test_edge_also_seen.py— impacttests/test_explain_same_file_tie.py— impacttests/test_extract.py— impacttests/test_file_label_disambiguation.py— impacttests/test_graph_context_cache_reload.py— impacttests/test_markdown_sections.py— impacttests/test_mcp_graph_view_cache.py— impacttests/test_path_cli.py— impacttests/test_path_confidence.py— impacttests/test_path_type_member.py— impacttests/test_query_induced_edges.py— impacttests/test_query_mcp_direction.py— impacttests/test_query_names_its_graph.py— impacttests/test_query_seed_order.py— impacttests/test_query_seeds.py— impacttests/test_query_source_first.py— impacttests/test_serve.py— impacttests/test_serve_http.py— impacttests/test_serve_reload_off_loop.py— impact, changed-testtests/test_terraform.py— impacttests/test_truncation_full_answer.py— impact
Selection is safe under the controlled-regression assumption; always-run tests + a periodic full run are the backstops. Advisory — it never changes the check verdict.
Docs that may be stale (advisory)
CHANGELOG.md§ 0.9.114 (2026-11-20) (lines 5-29): references changed symbolscallCHANGELOG.md§ 0.9.113 (2026-11-19) (lines 30-53): references changed symbolscallCHANGELOG.md§ 0.9.110 (2026-11-15) (lines 85-98): references changed symbolscallCHANGELOG.md§ 0.9.107 (2026-11-11) (lines 129-144): references changed symbolscallCHANGELOG.md§ 0.9.105 (2026-11-09) (lines 164-190): references changed symbolscallCHANGELOG.md§ 0.9.103 (2026-11-05) (lines 219-240): references changed symbolscallCHANGELOG.md§ 0.9.101 (2026-11-02) (lines 272-295): references changed symbolscallCHANGELOG.md§ 0.9.97 (2026-10-27) (lines 352-377): references changed symbolscallCHANGELOG.md§ 0.9.96 (2026-10-25) (lines 378-408): references changed symbolscallCHANGELOG.md§ 0.9.94 (2026-10-22) (lines 435-456): references changed symbolscall
…and 10 more.
Every flagged function
_build_server()ingraphify/serve.py:2906· 3 callers, 22 callees: fans out to 22 callees (efferent coupling)read_resource()ingraphify/serve.py:3475· 1 callers, 8 callees: fans out to 8 callees (efferent coupling)
CI runs mcp 2.x, whose Server has no public request_handlers. Look the handler up with get_request_handler() there, and keep request_handlers on 1.x.
Maintainer tweaks on top of the #5299/#5394/#5315/#5374 batch: - Move `import anyio` in serve.py to module top (was re-imported per call inside _warm_graph), a trivial cleanup on #5374. - 0.9.117 changelog and version bump. No AST-cache schema change in this batch (query scoring, export filenames, serve reload, rcfile parsing). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
|
Landed in v0.9.118 via an authorship-preserving cherry-pick. The MCP server now loads graph.json off the event loop, so a large reload no longer blocks concurrent requests; a request during a reload is served the current graph and a corrupt reload leaves the old graph intact. One tiny maintainer cleanup: hoisted the anyio import to module top. Thanks @Deepjyoti-Sarmah! |
What does this PR do?
Fixes #5371. While
graph.jsonreloads, MCP requests no longer wait for it. They are answered from the graph already in memory. Only the request that triggered the reload waits.#5181 (for #5177) already made
_GraphContextCache.load()return the stale entry to a second caller while a rebuild is in flight. That branch could never run in the server.call_toolandread_resourceare coroutines that call_select_graph()→load()→_load_entry()(parse + trigram index) synchronously on the event loop. Nothing else is dispatched until the load returns, so there is never a second caller.The new
_warm_graph()runs only the load in a worker thread.Gis still bound synchronously right before the handler, with noawaitin between, so another call cannot swap the sharedGin the middle of a handler.I also tried running
_select_graphitself in the thread. It passes the new tests but answers from the wrong graph: 5 of 40 calls in a two-project race probe. It rebindsGacross anawait, so another call can swap it before the handler runs. This PR got 0 of 40 in the same probe.Type of change
Verification & Invariants
Invariant: a request that arrives while a graph reloads is answered from that graph's current entry without waiting, and the handler always sees the graph its own request selected.
Merge Danger
Door: two-way. Reverting 58a7339 restores v8's behaviour.
Blast Radius: small.
call_toolandread_resourceingraphify/serve.py, for stdio and HTTP alike.anyiois already anmcpdependency.Limitations
How was this tested?
There's a new file,
tests/test_serve_reload_off_loop.py. It drives the server's own MCP request handlers concurrently, with_load_entryslowed to 0.8 s. Each request is timed from its planned arrival time: timing from when the coroutine starts would hide the stall.Here is an end-to-end run with the issue's stdio harness (from #5177) on a generated 80k-node / 160k-edge graph (22 MB). It sends 3
graph_statscalls 0.2 s apart, right aftertouch graph.json:I ran the full suite for v8 and this branch at the same time in the same venv:
ruff check graphify/serve.py tests/test_serve_reload_off_loop.py-> All checks passed!The new tests also fail on v8's
serve.pyand pass with the fix under mcp 2.3.0, the version CI uses: the helper dispatches throughget_request_handler()there andrequest_handlerson 1.x.Not tested: Windows.
Graphify-specific checklist
uv run python -m tools.skillgen --bless) when changing their source fragments. (N/A: no skill fragments touched.)